﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ESpace
{
    public partial class FormEdit : Form
    {
        bool bOk = false;
        public string RetVal { get { return txbValue.Text; } }

        public FormEdit()
        {
            InitializeComponent();
        }

        public delegate bool CloseDelegate(object sender, String RetVal);
        CloseDelegate IsClose = null;

        public bool ShowDialog(String Caption, String InitialValue, CloseDelegate IsClose)
        {
            Text = Caption;
            txbValue.Text = InitialValue;
            this.IsClose = IsClose;
            bOk = false;
            ShowDialog();
            return bOk;
        }

        private void txbValue_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.IsInputKey = false;
                btnOk_Click(null, null);
            }

            if (e.KeyCode == Keys.Escape)
            {
                e.IsInputKey = false;
                btnCancel_Click(null, null);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (IsClose(this, RetVal))
            {
                bOk = true;
                Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

    }
}
